using System.Collections.Generic;
using BasicScript.Component;
using Game.Main.Model;
using UnityEngine;

namespace Game.Component
{
	public class OperationECGItem : BaseItem
	{
		private const int MIN_NUM = 0;

		private const int MAX_NUM = 99;

		private bool isRunning;

		private float timer;

		private int num1;

		private int num2;

		private int num3;

		private int num4;

		private Transform num1Trans;

		private Transform num2Trans;

		private Transform num3Trans;

		private Transform num4Trans;

		private Dictionary<Transform, SpriteRenderer[]> numDic = new Dictionary<Transform, SpriteRenderer[]>();

		private SpriteAssets spriteAssets;

		private FrameAnimation ecg;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			spriteAssets = model.GetComponent<SpriteAssets>();
			num1Trans = model.Find("Num1");
			num2Trans = model.Find("Num2");
			num3Trans = model.Find("Num3");
			num4Trans = model.Find("Num4");
			ecg = model.Find("ECG").GetComponent<FrameAnimation>();
		}

		protected override void LateUpdate()
		{
			base.LateUpdate();
			if (isRunning)
			{
				timer += Time.deltaTime;
				if (timer >= 0.2f)
				{
					timer = 0f;
					num1 = Mathf.Clamp(num1 + Random.Range(-2, 3), 0, 99);
					num2 = Mathf.Clamp(num2 + Random.Range(-2, 3), 0, 99);
					num3 = Mathf.Clamp(num3 + Random.Range(-2, 3), 0, 99);
					num4 = Mathf.Clamp(num4 + Random.Range(-2, 3), 0, 99);
					SetNumber(num1, num1Trans);
					SetNumber(num2, num2Trans);
					SetNumber(num3, num3Trans);
					SetNumber(num4, num4Trans);
				}
			}
		}

		public void SetNumber(int num, Transform numTrans)
		{
			string[] array = new string[2];
			if (num < 10)
			{
				array[0] = "0";
				array[1] = num.ToString();
			}
			else
			{
				array[0] = num.ToString().Substring(0, 1);
				array[1] = num.ToString().Substring(1, 1);
			}
			SpriteRenderer[] array2 = new SpriteRenderer[2];
			if (numDic.ContainsKey(numTrans))
			{
				array2 = numDic[numTrans];
			}
			else
			{
				array2 = new SpriteRenderer[2]
				{
					numTrans.Find("0").GetComponent<SpriteRenderer>(),
					numTrans.Find("1").GetComponent<SpriteRenderer>()
				};
				numDic.Add(numTrans, array2);
			}
			for (int i = 0; i < array.Length; i++)
			{
				string text = array[i];
				array2[i].sprite = spriteAssets.GetSprite(text);
			}
		}

		public void IsRunning(bool val)
		{
			if (isRunning != val)
			{
				isRunning = val;
				if (isRunning)
				{
					num1 = Random.Range(10, 30);
					num2 = Random.Range(30, 60);
					num3 = Random.Range(60, 90);
					num4 = Random.Range(10, 89);
					ecg.Play();
					PlayAudio("sound_ecg");
				}
				else
				{
					num1 = 0;
					num2 = 0;
					num3 = 0;
					num4 = 0;
					ecg.GotoFirstFrame();
					ecg.Stop();
					RemoveAudioPlayer();
				}
				SetNumber(num1, num1Trans);
				SetNumber(num2, num2Trans);
				SetNumber(num3, num3Trans);
				SetNumber(num4, num4Trans);
				timer = 0f;
			}
		}
	}
}
